local skel = fk.CreateSkill {
  name = "lb__pitai",
  max_branches_use_time = {
    ["hp"] = {
      [Player.HistoryRound] = 1
    },
    ["handcard"] = {
      [Player.HistoryRound] = 1
    },
  },
}

Fk:loadTranslationTable {
  ["lb__pitai"] = "否泰",
  [":lb__pitai"] = "每轮各限一次，你体力值或手牌数变化至唯一最少时，可将之调整至另一值。",

  ["#lb__pitai_recover"] = "否泰：你可以回复 %arg 点体力",
  ["#lb__pitai_lose"] = "否泰：你可以失去 %arg 点体力",
  ["#lb__pitai_draw"] = "否泰：你可以摸 %arg 张牌",
  ["#lb__pitai_dis"] = "否泰：你可以弃置 %arg 张手牌",

  ["$lb__pitai1"] = "成象，效法！",
  ["$lb__pitai2"] = "极数，知来！",
  ["$lb__pitai3"] = "否极泰来。",
  ["$lb__pitai4"] = "上下象易。",
}

skel:addEffect(fk.HpChanged, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
        and player.hp ~= player:getHandcardNum()
        and not table.find(player.room:getOtherPlayers(player), function(to)
          return to.hp <= player.hp
        end)
        and skel:withinBranchTimesLimit(player, "hp", Player.HistoryRound)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = player.hp - player:getHandcardNum()
    local prompt = "#lb__pitai_lose:::" .. num
    local lose = true
    if num < 0 then
      num = math.abs(player.hp - math.min(player:getHandcardNum(), player.maxHp))
      prompt = "#lb__pitai_recover:::" .. num
      lose = false
    end
    if room:askToSkillInvoke(player, { skill_name = skel.name, prompt = prompt }) then
      event:setCostData(self, { num = num, lose = lose })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "hp")
    local room = player.room
    local num = event:getCostData(self).num
    player:broadcastSkillInvoke(skel.name)
    if event:getCostData(self).lose then
      room:notifySkillInvoked(player, skel.name, "negative")
      room:loseHp(player, num, skel.name)
    else
      room:notifySkillInvoked(player, skel.name, "support")
      room:recover {
        who = player,
        num = num,
        skillName = skel.name,
        recoverBy = player,
      }
    end
  end,
}, { check_skill_limit = true })

skel:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and skel:withinBranchTimesLimit(player, "handcard", Player.HistoryRound) then
      local sp = false
      for _, move in ipairs(data) do
        if move.from and move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              sp = true
              break
            end
          end
        end
      end
      return sp and not table.find(player.room:getOtherPlayers(player), function(to)
        return to:getHandcardNum() <= player:getHandcardNum()
      end) and player.hp ~= player:getHandcardNum()
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#lb__pitai_draw:::" .. player.hp - player:getHandcardNum()
    local darw = true
    if player.hp < player:getHandcardNum() then
      prompt = "#lb__pitai_dis:::" .. player:getHandcardNum() - player.hp
      darw = false
    end
    if darw then
      return room:askToSkillInvoke(player, { skill_name = skel.name, prompt = prompt })
    else
      local cards = room:askToDiscard(player, {
        min_num = player:getHandcardNum() - player.hp,
        max_num = player:getHandcardNum() - player.hp,
        skill_name = skel.name,
        prompt = prompt,
        cancelable = true,
        include_equip = false,
        skip = true,
      })
      if #cards > 0 then
        event:setCostData(self, { cards = cards })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "handcard")
    local room = player.room
    player:broadcastSkillInvoke(skel.name)
    if event:getCostData(self) and event:getCostData(self).cards then
      room:notifySkillInvoked(player, skel.name, "negative")
      room:throwCard(event:getCostData(self).cards, skel.name, player)
    else
      room:notifySkillInvoked(player, skel.name, "drawcard")
      player:drawCards(player.hp - player:getHandcardNum(), skel.name)
    end
  end,
}, { check_skill_limit = true })

return skel
